package com.game.eggscatcher.GameWorld;


import java.util.Random;

import sun.org.mozilla.javascript.internal.ast.SwitchStatement;

import com.badlogic.gdx.utils.Array;
import com.game.eggscatcher.GameHelpers.AssetLoader;
import com.game.eggscatcher.GameObjects.GiaCam;
import com.game.eggscatcher.GameObjects.Ro;


public class GameWorld {
	private Random random;
	private Array<GiaCam> ga = new Array<GiaCam>();
	private Array<GiaCam> vit = new Array<GiaCam>();
	private Array<GiaCam> ngan = new Array<GiaCam>();
	private Ro ro;
	private int currentScore = 0;
	public int getCurrentScore() {
		return currentScore;
	}
	public enum GameStates {
		READY, RUNNING, GAMEOVER, HIGHSCORE, LEVELUP
	}
	private GameStates currentState;
	private int level = 1;
	public boolean isHighScore = false;
	
	public int getLevel() {
		return level;
	}
	public GameWorld() {
		currentState = GameStates.READY;
		random = new Random();
		ro = new Ro(58, 170);
		addGa(level);
		addVit(level);
		addNgan(level);
		
	}
	public void update(float delta) {
		switch (currentState) {
		case READY:
			updateReady(delta);
			break;
		case RUNNING:
			updateRunning(delta);
			break;

		default:
			break;
		}
	}
	private void updateReady(float delta) {
		
	}
	private void updateRunning(float delta) {
		
		for (int i = 0; i < ga.size; i++) {
			ga.get(i).update(delta);
		}
		for (int i = 0; i < vit.size; i++) {
			vit.get(i).update(delta);
		}
		for (int i = 0; i < ngan.size; i++) {
			ngan.get(i).update(delta);
		}
//		vit.update(delta);
	}
	
	public void startGame() {
		currentState = GameStates.RUNNING;
	}
	public void restartGame() {
		
		if (currentState == GameStates.LEVELUP) ro.levelUp();
		else ro.reset(); 
		ga.clear();
		addGa(level);
		vit.clear();
		addVit(level);
		ngan.clear();
		addNgan(level);
		isHighScore = false;
		currentState = GameStates.RUNNING;
	}
	public void gameOver() {
		
		currentState = GameStates.GAMEOVER;
		level = 1;
		currentScore = ro.getScore();
		if (currentScore > AssetLoader.getHighScore()) {
			AssetLoader.setHighScore(currentScore);
			isHighScore  = true;
		}
	}
	
	public boolean isReady() {
		return currentState == GameStates.READY;
	}
	public boolean isRunning() {
		return currentState == GameStates.RUNNING;
	}
	public boolean isGameOver() {
		return currentState == GameStates.GAMEOVER;
	}
	public Ro getRo() {
		return ro;
	}
	public GiaCam getGa(int index) {
		return ga.get(index);
	}
	public GiaCam getVit(int index) {
		return vit.get(index);
	}
	public GiaCam getNgan(int index) {
		return ngan.get(index);
	}
	public int getCountGa() {
		return ga.size;
	}
	public int getCountVit() {
		return vit.size;
	}
	public int getCountNgan() {
		return ngan.size;
	}
	public void addGa(int level) {
		int x = random.nextInt(100);
		int v = random.nextInt(30)+20;
		ga.add(new GiaCam(x, 15, v, level, this));
		
	}
	public void addVit(int level) {
		int x = random.nextInt(100);
		int v = random.nextInt(30)+20;
		vit.add(new GiaCam(x, 30, v, level, this));
		
	}
	public void addNgan(int level) {
		int x = random.nextInt(100);
		int v = random.nextInt(30)+20;
		ngan.add(new GiaCam(x, 45, v, level, this));
		
	}
	public Array<GiaCam> getListGa() {
		return ga;
	}
	
	public Array<GiaCam> getListVit() {
		return vit;
	}
	
	public Array<GiaCam> getListNgan() {
		return ngan;
	}
	public void gainLevel() {
		currentState = GameStates.LEVELUP;
		level++;
	}
	public boolean isLevelUp() {
		return currentState == GameStates.LEVELUP;
	}
}
